package day13.lianxi;

/**
 * @Auther:小陈的Java
 */
public class AccountTest2 {
    public static void main(String[] args) {
        Account2 account2 = new Account2(0);
        Runnable runnable = new Customer2(account2);
        Thread t1 = new Thread(runnable,"1");
        Thread t2 = new Thread(runnable,"2");
        t1.start();
        t2.start();

    }
}
class Account2 {
    private static int money;

    public Account2(int money) {
        this.money = money;
    }

    public void saveMoney(int money) {
        synchronized (this) {
            if (money > 0) {
                this.money += money;
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + "：存钱成功。余额为：" + this.money);
            }
        }
    }
}
class Customer2 implements Runnable {
    private Account2 account2;
    public Customer2(Account2 account2) {
        this.account2 = account2;
    }
    @Override
    public void run() {
        for (int i = 1; i <=3; i++) {
            account2.saveMoney(1000);
        }
    }
}